package com.study.manage.mvc;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.study.manage.constant.Url;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.mvc.common.BaseController;
import com.study.manage.utils.HttpClient;


@Controller
@RequestMapping("/download")
public class AppController extends BaseController {


    @SuppressWarnings({"unchecked", "rawtypes"})
    @RequestMapping("app")
    public String download(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) {
        try {
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            String uagent = request.getHeader("User-Agent");
            boolean isWeiXin = uagent.indexOf("MicroMessenger") > -1;
            boolean isWeibo = uagent.indexOf("Weibo") > -1;

            boolean isAndroid = uagent.indexOf("Android") > -1 || uagent.indexOf("Linux") > -1;
            boolean isiPhone = uagent.indexOf("iPhone") > -1 || uagent.indexOf("iPad") > -1;
            logger.info("request head:" + uagent);
            if ((isWeiXin || isWeibo) && isAndroid) {
                model.put("type", "android");
            } else if ((isWeiXin || isWeibo) && isiPhone) {
                model.put("type", "ios");
            } else if (isAndroid) {
                Map<String, Object> map = new HashMap<>();
                map.put("version", "");
                map.put("platform", "android");
                AjaxResponse res = HttpClient.get(Url.getUrl(serverUrl, Url.VERSIONS), map);
                Map<String, Object> resMap =
                        jsonMapper.fromJson(res.getObj().toString(), Map.class);
                Map<String, Object> dataMap = (Map) resMap.get("data");
                if (dataMap.containsKey("url")) {
                    response.sendRedirect(dataMap.get("url").toString());
                    return null;
                } else {
                    model.put("errorInfo", "我要学安卓版即将推出");
                }
            } else if (isiPhone) {
                Map<String, Object> map = new HashMap<>();
                map.put("version", "");
                map.put("platform", "ios");
                AjaxResponse res = HttpClient.get(Url.getUrl(serverUrl, Url.VERSIONS), map);

                Map<String, Object> resMap =
                        jsonMapper.fromJson(res.getObj().toString(), Map.class);
                Map<String, Object> dataMap = (Map) resMap.get("data");
                if (dataMap.containsKey("url")) {
                    String url = dataMap.get("url").toString();
                    if (StringUtils.isEmpty(url)) {
                        url = "https://itunes.apple.com/app/id1099608674";
                    }
                    response.sendRedirect(url);
                    return null;
                } else {
                    model.put("errorInfo", "我要学苹果版即将推出");
                }
            } else {
                model.put("errorInfo", "用手机打开");
            }

        } catch (IOException e) {
            logger.error("系统异常", e);
        }
        return "download";
    }

}
